using System;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
	/**
	 * Classname : Nguoidung
	 * Version information :1.0
	 * Date  :26-09-2006
	 * Copyright notice : 
	 */

    public class DAChucNang : DbObject
	{
        protected SqlConnection connec;

        public DAChucNang(SqlConnection Connection)
		{
            connec = Connection;
		}	
		// Kiem tra mat khau nguoi dung
        public DataSet LayDanhSachChucNang()
		{
			// create the parameter
            DataSet dsLevel = RunProcedure("sp_ChucNang_LayDanhSachChucNang", "TB_ChucNang", connec);
            return dsLevel;
		}
        // return only the record with the specified ID!!!!!!!!!!!
        public DataRow LayDanhSachChucNangID(int LevelID)
        {
            // create the parameter
            SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = LevelID;

            using (DataSet dsLevel = RunProcedure("sp_ChucNang_LayDanhSachChucNangID", parameters, "TB_ChucNang", connec))
            {
                return dsLevel.Tables[0].Rows[0];
            }
        }
        public DataSet LayDanhSachChucNangCha()
        {
            //create the parameter
            DataSet dsChucNangCha = RunProcedure("sp_Quyen_loadChucNangCha", "TB_ChucNang", connec);
            for (int i = 0; i < dsChucNangCha.Tables[0].Rows.Count; i++)
            {
                dsChucNangCha.Tables[0].Rows[i]["seq"] = (i + 1).ToString();
            }
            return dsChucNangCha;
        }
        public int ThemDanhSachChucNang(string i_str_tencn
            ,string i_str_ten_form
            , string i_str_trangthai
            ,int i_i_vitri
            ,int i_i_cnparent
            ,string i_str_hienthi)
        {
            SqlParameter[] parameters = { 
                                            new SqlParameter("@TenChucNang",SqlDbType.NVarChar,100),
                                            new SqlParameter("@TenForm",SqlDbType.NVarChar,100),
                                            new SqlParameter("@TrangThai",SqlDbType.NVarChar,100),
                                            new SqlParameter("@ViTri",SqlDbType.Int,100),
                                            new SqlParameter("@ChucNangParent",SqlDbType.Int,100),
                                            new SqlParameter("@Hienthi",SqlDbType.NVarChar,100),
                                        };
            parameters[0].Value = i_str_tencn;
            parameters[1].Value = i_str_ten_form;
            parameters[2].Value = i_str_trangthai;
            parameters[3].Value = i_i_vitri;
            parameters[4].Value = i_i_cnparent;
            parameters[5].Value = i_str_hienthi;
            RunProcedure("[sp_Quyen_InsertChucNang]", parameters, "TB_ChucNang", connec);
            return 1;
        }
        public int XoaDanhSachChucNang(int i_i_idcon)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@ID",SqlDbType.Int,18)
                                        };
            parameters[0].Value = i_i_idcon;
            DataSet v_ds_chuc_nang_con = RunProcedure("sp_Quyen_DeleteChucNang", parameters, "TB_QuyenChucnang", connec);
            return 1;
        }
        public DataSet LayDanhSachChucNangCon(int i_i_idcha)
        {
            SqlParameter[] parameters = {
                                           new SqlParameter("@ID",SqlDbType.Int)
                                       };
            parameters[0].Value = i_i_idcha;
            DataSet v_ds_chuc_nang_con = RunProcedure("sp_ChucNang_LayDanhSachCon", parameters,"TB_ChucNang", connec);
            return v_ds_chuc_nang_con;
        }
        public DataRow Lay_hang_chuc_nang_by_id(int i_i_id)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@ID",SqlDbType.Int)
                                        };
            parameters[0].Value = i_i_id;
            DataSet v_ds_lay_hang_chuc_nang = RunProcedure("sp_Quyen_loadChucNangByID", parameters, "TB_ChucNang", connec);
            return v_ds_lay_hang_chuc_nang.Tables[0].Rows[0];
        }
        public int CapNhatChucNang(int i_i_id
            ,string i_str_ten_chuc_nang
            ,string i_str_ten_form
            ,string i_str_trang_thai
            ,int i_i_vi_tri
            ,string i_str_chuc_nang_parent
            ,string i_str_hien_thi)
        {
            SqlParameter[] parameters = {
                                            new SqlParameter("@ID",SqlDbType.Int,18)
                                            ,new SqlParameter("@TenChucNang",SqlDbType.NVarChar,100)
                                            ,new SqlParameter("@TenForm",SqlDbType.NVarChar,100)
                                            ,new SqlParameter("@TrangThai",SqlDbType.NVarChar,100)
                                            ,new SqlParameter("@ViTri",SqlDbType.Int,4)
                                            ,new SqlParameter("@ChucNangParent",SqlDbType.NVarChar,100)
                                            ,new SqlParameter("@Hienthi",SqlDbType.NVarChar,100)
                                        };
            parameters[0].Value = i_i_id;
            parameters[1].Value = i_str_ten_chuc_nang;
            parameters[2].Value = i_str_ten_form;
            parameters[3].Value = i_str_trang_thai;
            parameters[4].Value = i_i_vi_tri;
            parameters[5].Value = i_str_chuc_nang_parent;
            parameters[6].Value = i_str_hien_thi;
            DataSet v_ds_cap_nhat_danh_sach_by_id = RunProcedure("[sp_Quyen_UpdateChucNang]", parameters, "TB_ChucNang", connec);
            return 1;
        }
	}
}